<?php
class Rating
{
	
	var $rating;
	var $isDisable = false;
	var $avgRating;
	//data base field
	var $target_id; 

	var $ipAddress;
	var $oldIPs = Array();
	var $total_votes = 0;
	var $total_value = 0;	
	var $id;
	
	var $maxPoint = 5;
	var $options = array(
		"0.5" => 'Very poor',
		"1.0" => 'Poor',
		"1.5" => 'Not that bad',
		"2.0" => 'Fair',
		"2.5" => 'Average',
		"3.0" => 'Almost good',
		"3.5" => 'Good',
		"4.0" => 'Very good',
		"4.5" => 'Excellent',
		"5.0" => 'Perfect'
		);
	
	function getRating()
	{
		$sql = "select * from ratings where target_id=".addslashes($this->target_id);
		$this->rating = db_fetch_one($sql);	
		$this->ipAddress = getRealIpAddress();
		
		if($this->rating)
		{
			$this->oldIPs = unserialize($this->rating->used_ips);
			$this->total_votes = $this->rating->total_votes;
			$this->total_value = $this->rating->total_value;
			$this->id = $this->rating->id;
		}
		if (in_array($this->ipAddress, $this->oldIPs)) 
			$this->isDisable = true;
	}
	
	
	function updateRating($value)
	{
		if($value>$this->maxPoint || $value<0)
			return;
		$this->getRating();
		
		if($this->rating)
		{//update
			if (!$this->isDisable) 
			{				
				$data["total_votes"] = $this->total_votes + 1;
				$data["total_value"] = $this->total_value + $value ;
				array_push($this->oldIPs, $this->ipAddress); 
				$data["used_ips"] = serialize($this->oldIPs) ;
				db_update("ratings",$this->id,$data);
			}
			
		}else
		{//isert new
			$data["target_id"] =  $this->target_id;
			
			$data["total_votes"] =  1;
			$data["total_value"] = ($value > 0)? $value : 1;
			array_push($this->oldIPs, $this->ipAddress); 
			$data["used_ips"] = serialize($this->oldIPs);
			db_insert("ratings",$data);
			
		}
		
		$avg = ($data["total_votes"] > 0)? ($data["total_value"] / $data["total_votes"] ) : 0 ;
		$avg = round($avg,1);
		
		$channel_data["vote"] = $avg;
		db_update("channel",$this->target_id,$channel_data);
		
		$result = array(
			'votes' => $data["total_votes"] ,
			'sum' => $data["total_value"], 
			'avg' => $avg
		);
		return $result;
		
	}
	
	function showRating()
	{
		$strRating = "";
		$this->getRating();
		
		if($this->isDisable)
			return  $this->showAvgRating();
		
		$disable = (($this->isDisable)?" disabled='disabled' ":"");
		$avgRating = ($this->total_votes>0)? ($this->total_value / $this->total_votes) : 0 ;
		$this->avgRating = round($avgRating,1);
		
		$name="star_".$this->target_id;
		foreach ( $this->options  as $id => $title){
			if(roundNearestHalf($avgRating) == $id)
				$checked = "checked='checked'";
			else $checked="";	
			
			$strRating .= "<input type='radio' name='$name' value='$id' title='$title' $checked  $disable />";
		}
		
			
		return $strRating;
	}

	function showAvgRating()
	{
		$strRating = "";
		$this->getRating();
		
		$avgRating = ($this->total_votes>0)? ($this->total_value / $this->total_votes) : 0 ;
		$avgRating = round($avgRating,1);
		$numStar = 5;
		$width_stars_off = 110; //px
		$width_stars_on = round($width_stars_off / $numStar * $avgRating) ; 
		
		$strRating = "<div class='stars-off' style='width:".$width_stars_off."px'><div class='stars-on'  style='width:".$width_stars_on."px'></div></div>";
		
		return $strRating;
	}
}

?>